package cate.game.role.res.guild.po.my.skill;

import cate.common.table.d.GDGuild;
import cate.common.table.guild.GuildSkillRow;
import cate.game.attr.FightAttr;
import cate.game.res.MixRes;
import cate.game.res.MixResAdder;
import cate.game.res.MixResItem;
import cate.game.role.Role;
import cate.game.role.RoleDependent;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;
import easy.java.practice.errorcode.EcResult;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@NoteClass(value = "阵营普通技能升级")
public class GuildJobSkill extends RoleDependent {

    @NoteField(value = "职业")
    public byte job;
    @NoteField(value = "不同顺位对应的等级", detail = "Map<位置order,等级level>")
    public Map<Integer, Integer> map;

    @Override
    public void initialize(Role role) {
        super.initialize(role);
        if (map == null) {
            map = new HashMap<>();
            mapRefresh();
        }
    }

    public void mapRefresh() {
        for (int order = 1; order < 7; order++) {
            map.put(order, GDGuild.Skill.PVP_SKILL_ORIGIN_LEVEL);
        }
    }

    public GuildJobSkill() {
    }

    public GuildJobSkill(byte job) {
        this.job = job;
    }

    public boolean isValidForUpgrade(int requireLevel) {
        for (int order : map.keySet()) {
            GuildSkillRow row = onGetJobSkillRow(order);
            if (row == null) {
                role.getGame().notice.message(role, "配置错误");
                return false;
            }
            if (row.isSkill) {
                continue;
            }
            if (requireLevel > map.get(order)) {
                return false;
            }
        }
        return true;
    }


    private GuildSkillRow onGetJobSkillRow(int order) {
        return role.getGame().table.guild.skill.onGetRow(GDGuild.Skill.PVP_SKILL_TYPE, job, order, map.get(order));
    }


    public void back(boolean firstBack) {
//        MixRes cost = new MixRes();
        MixRes reward = new MixRes();
        for (int order : map.keySet()) {
            GuildSkillRow currentRow = onGetJobSkillRow(order);
            if (currentRow == null) {
                role.getGame().notice.message(role, "配置错误");
                return;
            }
//            cost.addList(new MixRes(currentRow.backCostStr));
            reward.addList(new MixRes(currentRow.backRewardStr));
        }
//        if (firstBack) {
//            EcResult<MixResItem> consumeRes = new MixRes(GDGuild.Skill.FIRST_COST_FOR_NORMAL_SKILL_BACK)
//                    .consume(role, null);
//            if (!consumeRes.ok()) {
//                role.getGame().notice.tipResItem(role, consumeRes.data);
//                return;
//            }
//        } else {
//            EcResult<MixResItem> consumeRes = cost.consume(role, null);
//            if (!consumeRes.ok()) {
//                role.getGame().notice.tipResItem(role, consumeRes.data);
//                return;
//            }
//        }
        EcResult<MixResItem> consumeRes = new MixRes(GDGuild.Skill.COST_FOR_PVP_SKILL_BACK)
                .consume(role, null);
        if (!consumeRes.ok()) {
            role.getGame().notice.tipResItem(role, consumeRes.data);
            return;
        }

        new MixResAdder().setRes(reward)
                .setDialogReward(true)
                .exe(role);
//        role.getGame().notice.dialogReward(role, reward);
//        reward.add(role, new MergeMsgSender());
        mapRefresh();
    }

    public FightAttr onGetAttr() {
        FightAttr attr = new FightAttr();
        for (int order : map.keySet()) {
            GuildSkillRow row = onGetJobSkillRow(order);
            if (row == null) {
                role.getGame().notice.message(role, "配置错误");
                continue;
            }
            if (row.isSkill) {
                continue;
            }
            attr.add(new FightAttr(row.attrStr));
        }
        return attr;
    }

    public List<Integer> onGetList() {
        List<Integer> list = new ArrayList<>();
        for (int order : map.keySet()) {
            GuildSkillRow row = onGetJobSkillRow(order);
            if (row == null) {
                role.getGame().notice.message(role, "配置错误");
                continue;
            }
            if (row.isSkill) {
                list.add(row.skillId);
            }
        }
        return list;
    }

    public void gmHighestLevel(List<GuildSkillRow> typeList) {
        for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
            int levelMax = typeList.stream()
                    .filter(row -> row.order == entry.getKey())
                    .mapToInt(row -> row.level)
                    .max()
                    .orElse(0);
            entry.setValue(levelMax);
        }

    }

    public void initialize(byte job) {
        this.job = job;
    }
}
